﻿<template>
	<div class="housing-container">
		<el-dialog v-model="state.isShowDialog" :width="800" draggable="" :close-on-click-modal="false">
			<template #header>
				<div style="">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle">
						<Edit />
					</el-icon>
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-tabs v-loading="state.loading" v-model="state.selectedTabName">
				<el-tab-pane label="基础信息">
					<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
						<el-row :gutter="20">
							<el-form-item v-show="false">
								<el-input v-model="state.ruleForm.id" />
								<el-input v-model="state.ruleForm.villageId" />
								<el-input v-model="state.ruleForm.floorId" />
								<el-input v-model="state.ruleForm.rentType" />
							</el-form-item>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="层级信息" prop="floorTitle">
									<el-input v-model="props.floorTitle" placeholder="" readonly maxlength="100" />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="房间编号" prop="roomNo">
									<el-input v-model="state.ruleForm.roomNo" placeholder="请输入房间编号" maxlength="100"
										show-word-limit clearable />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="住房面积" prop="roomArea">
									<el-input-number v-model="state.ruleForm.roomArea" placeholder="请输入住房面积" :precision="2"
										:step="0.1" />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="建筑面积" prop="buildingArea">
									<el-input-number v-model="state.ruleForm.buildingArea" placeholder="请输入建筑面积" :precision="2"
										:step="0.1"  />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="月租金" prop="monthlyRent">
									<el-input-number v-model="state.ruleForm.monthlyRent" placeholder="请输入月租金" :precision="2"
										:step="0.1"  />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="停保月租金" prop="tBMonthlyRent">
									<el-input-number v-model="state.ruleForm.tbMonthlyRent" placeholder="请输入停保月租金" :precision="2"
										:step="0.1"  />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="配租办公室" prop="rentingOffice">
									<el-input v-model="state.ruleForm.rentingOffice" placeholder="请输入配租办公室" maxlength="200"
										show-word-limit clearable />
								</el-form-item>
							</el-col>
							<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="状态" prop="rState">
									<el-input-number v-model="state.ruleForm.rState" placeholder="请输入状态" clearable />
								</el-form-item>
							</el-col> -->
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="房屋类型" prop="rentType">
									<el-select v-model="state.ruleForm.rentType" placeholder="房屋类型" class="w100" readonly>
										<el-option label="未知" :value="-1" />
										<el-option label="廉租房" :value="0" />
										<el-option label="政企合建廉租房" :value="2" />
										<el-option label="经济适用房" :value="3" />
										<el-option label="公租房【计划内】" :value="4" />
										<el-option label="公租房【计划外】" :value="5" />
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
								<el-form-item label="备注" prop="remarks">
									<el-input v-model="state.ruleForm.remarks" placeholder="请输入备注" maxlength="2000"
										type="textarea" show-word-limit clearable />
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
				</el-tab-pane>
				<el-tab-pane label="详细信息">
					<el-form :model="state.ruleForm" label-width="auto">
						<el-row :gutter="20">
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="公摊系数" prop="publicRatio">
									<el-input-number v-model="state.ruleForm.publicRatio" placeholder="请输入公摊系数" :precision="2"
										:step="0.1"  />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="公摊面积" prop="publicArea">
									<el-input-number v-model="state.ruleForm.publicArea" placeholder="请输入公摊面积" :precision="2"
										:step="0.1"  />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="基础租金" prop="baseRent">
									<el-input-number v-model="state.ruleForm.baseRent" placeholder="请输入基础租金" :precision="2"
										:step="0.1"  />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="结构系数" prop="structureRatio">
									<el-input-number v-model="state.ruleForm.structureRatio" placeholder="请输入结构系数"
										:precision="2" :step="0.1"  />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="配套系数" prop="matchingRatio">
									<el-input-number v-model="state.ruleForm.matchingRatio" placeholder="请输入配套系数" :precision="2"
										:step="0.1"  />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="区位系数" prop="locationRatio">
									<el-input-number v-model="state.ruleForm.locationRatio" placeholder="请输入区位系数" :precision="2"
										:step="0.1"  />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="楼层系数" prop="floorRatio">
									<el-input-number v-model="state.ruleForm.floorRatio" placeholder="请输入楼层系数" :precision="2"
										:step="0.1"  />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="停保系数" prop="tBRatio">
									<el-input-number v-model="state.ruleForm.tbRatio" placeholder="请输入停保系数" :precision="2"
										:step="0.1"  />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="房屋用途" prop="roomUse">
									<el-input v-model="state.ruleForm.roomUse" placeholder="请输入房屋用途" maxlength="100"
										show-word-limit clearable />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="房屋性质" prop="roomNature">
									<el-input v-model="state.ruleForm.roomNature" placeholder="请输入房屋性质" maxlength="100"
										show-word-limit clearable />
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
								<el-form-item label="房屋状态" prop="roomState">
									<el-input v-model="state.ruleForm.roomState" placeholder="请输入房屋状态" maxlength="100"
										show-word-limit clearable />
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
				</el-tab-pane>
			</el-tabs>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<script lang="ts" setup>
	import { ref,onMounted, reactive } from "vue";
	// import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
	import { ElMessageBox, ElMessage } from 'element-plus';
	import { Edit } from '@element-plus/icons-vue';
	import type { FormRules } from "element-plus";
	//import { useDictApi } from "/@/api/dict";
	import { useHousingApi } from "/@/api/housing";
	// import { getAPI } from '/@/utils/axios-utils';
	// import { SysEnumApi } from '/@/api-services/api';

	//const getEnumStatusData = ref<any>([]);
	//父级传递来的参数
	var props = defineProps({
		title: {
			type: String,
			default: "",
		},
		floorTitle:{
			type:String,
			default: ""
		}
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["handleQuery"]);
	const ruleFormRef = ref();

	const state = reactive({
		isShowDialog: false,
		ruleForm: {} as HousingForm,
		floorTitle:'',
		//getEnumStatusData: [],
		// ownOrgData: [],
	});
	// const isShowDialog = ref(false);
	// const ruleForm = ref<any>({});
	//自行添加其他规则
	const rules = ref<FormRules>({
		roomNo: [{required: true, message: '请输入小区名称！', trigger: 'blur',},],
		// rentType: [{required: true, message: '请选择小区（项目）类型！', trigger: 'change',},],
	});

	// 打开弹窗
	const openDialog = async (type: string, row: HousingForm) => {
		console.log('type:'+type+'	row:'+JSON.stringify(row));
		if(type==='edit'){

			await useHousingApi().getHousing({id:row.id}).then(async (res:AjaxResult)=>{
				const _data : HousingForm = res?.data;
				state.ruleForm = _data;
			});
		}else{
			state.ruleForm=row;
		}
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		// let rowData = JSON.parse(JSON.stringify(row));
		// if (rowData.id)
		// 	ruleForm.value = {}; //(await detailVillage(rowData.id)).data.result;
		// else
		// 	ruleForm.value = rowData;
		state.isShowDialog = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("handleQuery");
		state.isShowDialog = false;
	};

	// 取消
	const cancel = () => {
		state.isShowDialog = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				//let values = ruleForm.value;
				if (state.ruleForm.id == undefined || state.ruleForm.id == null || state.ruleForm.id == "" || state.ruleForm.id == 0) {
					// await addVillage(values);
					useHousingApi().addHousing(state.ruleForm).then(async (res:AjaxResult)=>{
						if (res.success == true) {
							ElMessage.success('更新成功');
							//emits('handleQuery');
							closeDialog();
						}
						else {
							ElMessage.warning('更新失败：' + res.message);
						}
					});
				} else {
					// await updateVillage(values);
					useHousingApi().editHousing(state.ruleForm).then(async (res:AjaxResult)=>{
						if (res.success == true) {
							ElMessage.success('更新成功');
							//emits('handleQuery');
							closeDialog();
						}
						else {
							ElMessage.warning('更新失败：' + res.message);
						}
					});
				}
				//closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};

	// 页面加载时
	onMounted(async () => {
			// state.getEnumStatusData = (await getAPI(SysEnumApi).apiSysEnumEnumDataListGet('StatusEnum')).data.result ?? [];
	});

	//将属性或者函数暴露给父组件
	defineExpose({ openDialog });
</script>
<style scoped>
	:deep(.el-select),
	:deep(.el-input-number) {
		width: 100%;
	}
</style>




